package com.ttg.utilspaidui;
import java.util.*;
import com.ttg.utils.*;
import net.sf.json.JSONObject;
/**
 Class TLinx2Util
 Description
 Create 2017-03-07 14:01:23
 @author Benny.YEE */
public class TLinx2Util{
    public static String getNonceStr(){
        Random random = new Random();
        return MD5Util.MD5Encode(String.valueOf(random.nextInt(10000)),"UTF-8");
    }
    // 排序
    public static String sortjsonWithOutNull(JSONObject jsonMap) throws Exception{
        Map<String,String> paramMap = new HashMap<String,String>();
        // 将json字符串转换成jsonObject
        JSONObject jsonObject = JSONObject.fromObject(jsonMap);
        Iterator ite = jsonObject.keys();
        // 遍历jsonObject数据,添加到Map对象
        while(ite.hasNext()){
            String key = ite.next().toString();
            String value = jsonObject.get(key).toString();
            if(!"null".equals(value)){
                paramMap.put(key,value);
            }
        }
        String sort = "";
        TLinxMapUtil signMap = new TLinxMapUtil();
        String key1;
        for(String s : paramMap.keySet()){
            key1 = s;
            String value = ((paramMap.get(key1) != null) && (!("".equals(paramMap.get(key1)))) && (!"null".equals(paramMap.get(key1)))) ? paramMap.get(key1) : "";
            signMap.put(key1,value);
        }
        signMap.sort();
        for(Object o : signMap.keySet()){
            key1 = (String)o;
            sort = sort + key1 + "=" + signMap.get(key1).toString() + "&";
        }
        if(!"".equals(sort)){
            sort = sort.substring(0,sort.length() - 1);
        }
        return sort;
    }
    /**
     验签
     */
    public static String signAes(JSONObject jsonMap,String encoding,String aesKey,String aesIv){
        String sortStr;
        String sign = "";
        try{
            //1 A~z排序(加上open_key)
            sortStr = sortjsonWithOutNull(jsonMap);
            System.out.println("sortStr " + sortStr);
            //2 sha1加密(大写)
            String sha1 = TLinxSHA1.SHA1(sortStr).toUpperCase();
            System.out.println("sha1 " + sha1);
            //3 AES加密(小写)
            sign = TlinxAES3.encrypt(sha1,encoding,aesKey,aesIv);
        } catch(Exception e){
            e.printStackTrace();
        }
        return sign;
    }
    /**
     验签
     @param respObject respObject
     @return boolean
     */
    public static Boolean verifyPaiDuiSignWithAes(JSONObject respObject,String encoding,String aesKey,String aesIv){
        String respSign = respObject.get("sign").toString();
        // 删除sign节点
        respObject.remove("sign");
        System.out.println("sign 1 " + respSign);
        // 按A~z排序，串联成字符串，先进行sha1加密(大写)，再进行AES加密(小写)，得到签名
        String veriSign = signAes(respObject,encoding,aesKey,aesIv);
        System.out.println("sign 1 " + respSign);
        System.out.println("sign 2 " + veriSign);
        return respSign.equals(veriSign);
    }
    /**
     请求接口
     @param postmap postmap
     @return 响应字符串
     */
    public static String handlePostPaiDui(TreeMap<String,String> postmap,String interfaceName){
        //String url = Constant.OPEN_URL+interfaceName;
        String url = Constant.TESTOPEN_URL + interfaceName;
        if(url.contains("https")){
            return HttpsUtil.httpMethodPost(url,postmap,"UTF-8");
        } else{
            return HttpUtil.httpMethodPost(url,postmap,"UTF-8");
        }
    }
}
